<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Redis - 持久化 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/52.44e69a41.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Redis - 持久化</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Redis - 持久化</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>4/20/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Redis</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="redis-的持久化"><a href="#redis-的持久化" class="header-anchor">#</a> Redis 的持久化</h2> <blockquote><p>首先我们要明确 Redis 为什么需要持久化？</p> <p>Redis 持久化 并不是为了 <code>保证数据的完全性</code>,  保证完整性我们完全可以使用 <strong>关系型数据库</strong> 去存储数据。而是为了保证服务 出现故障 或 重启后快速的进行<strong>故障恢复</strong></p></blockquote> <p><strong>持久化的机制</strong></p> <p>Redis 提供了两种持久化方式 <code>RDB</code> 和 <code>AOF</code></p> <h2 id="rdb"><a href="#rdb" class="header-anchor">#</a> RDB</h2> <h3 id="什么是rdb"><a href="#什么是rdb" class="header-anchor">#</a> 什么是RDB?</h3> <blockquote><p>Redis DateBase, 是redis 默认的存储方式，是通过 <code>快照</code> 完成的，即存储的是某一时刻的数据副本。 所生成的RDB文件是一个经过压缩的二进制文件</p></blockquote> <p><strong>RDB 不关注存储的过程，只关注某一时刻的最终结果数据</strong></p> <h3 id="触发生成快照时间-或-方式"><a href="#触发生成快照时间-或-方式" class="header-anchor">#</a> 触发生成快照时间 或 方式</h3> <ol><li>符合自定义配置（自动间隔保存）的快照规则 （服务有默认配置，可以进行修改）</li> <li>执行 <code>save</code> 或 <code>bgsave</code>命令</li> <li>执行 <code>flushall</code> 命令</li> <li>执行主从服务操作 （仅第一次操作时）</li></ol> <p><strong>配置自动间隔保存</strong></p> <blockquote><p>当redis服务部署完成后就会有默认的生成快照的规则，</p> <p>在 redis.conf 文件中进行配置: save 多少秒内 数据变了多少</p> <p>服务器 会使用周期性事件： <code>ServerCron</code> 每隔 100毫秒 执行一次。验证是否满足配合的规则</p></blockquote> <div class="language- extra-class"><pre class="language-text"><code># 默认 save &quot;&quot; 是注释掉的
save &quot;&quot; # 不使用RDB存储 不能主从

save 900 1          # 表示15分钟（900秒钟）内至少1个键被更改则进行快照。
save 300 10         # 表示5分钟（300秒）内至少10个键被更改则进行快照。
save 60 10000       # 表示1分钟内至少10000个键被更改则进行快照。
</code></pre></div><p><strong>注：</strong> 在配置文件中 save 命令的配置 其实运行时 和 bgsave 是一样的效果，会由子进程操作</p> <p>配置文件 Redis 服务启动会存储在 redisServer 中</p> <img src="/oliver-vuepress/middleware/redislearn/redisserver-saveparams.png" alt="foo"> <div class="language-c extra-class"><pre class="language-c"><code>	<span class="token keyword">struct</span> <span class="token class-name">redisServer</span> <span class="token punctuation">{</span>     
    <span class="token comment">// 记录保存的条件数组 设置的规则 都已数据形式存放</span>
    <span class="token keyword">struct</span> <span class="token class-name">saveparam</span> <span class="token operator">*</span>saveparams<span class="token punctuation">;</span>  
    <span class="token comment">// 修改计数器 （从上一次成功执行完 save 操作后 执行的修改次数）   </span>
    <span class="token keyword">long</span> <span class="token keyword">long</span> dirty<span class="token punctuation">;</span>     
    <span class="token comment">// 上一次执行保存的时间   </span>
    <span class="token class-name">time_t</span> lastsave<span class="token punctuation">;</span>   
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong><code>save</code> 和  <code>bgsave</code> 的区别？</strong></p> <ul><li><p>save：是一个同步保存操作,会堵塞redis服务进程,直到rdb文件创建完成为止, 进程堵塞期间不能处理任何命令请求</p></li> <li><p>bgsave: 是一个异步操作，bgsave命令会派生出一个子进程，由子进程负责创建rdb文件，父进程继续处理 redis命令</p></li></ul> <h3 id="生成-和-载入"><a href="#生成-和-载入" class="header-anchor">#</a> 生成 和 载入</h3> <h4 id="生成"><a href="#生成" class="header-anchor">#</a> 生成</h4> <img src="/oliver-vuepress/middleware/redislearn/bgsave操作.png" alt="foo"> <ol><li>客户端发送 bgsave 命令， Redis父进程首先判断是否有其他正在执行子进程（save | bgsave | bgrewriteaof）, 如果 有则：直接返回，无则：向下执行</li> <li>父进程执行 fork 操作创建 子进程 （在 fork 期间 父进程会堵塞，不会执行来自客户端的任何命令）</li> <li>父进程 fork后，bgsave命返回 &quot; Background saving started &quot; 信息并不再阻塞 Redis父进程，可以继续响应其他命令了。</li> <li>子进程，根据父进程内存快照生成临时RDB文件，完成后替换原来的 RDB 文件，同时发送信息给父进程表示RDB操作完成，父进程则更新统计信息</li></ol> <h4 id="载入"><a href="#载入" class="header-anchor">#</a> 载入</h4> <ol><li>服务器启动时自动加载检测是否有RDB文件存在</li> <li>若有则自动再载入RDB文件，在载入期间处于阻塞状态</li> <li>执行载入操作过，服务则可以对外响应请求</li></ol> <p><strong>如果 Reids 开启了 AOF 会优先使用AOF 文件来还原数据库状态</strong></p> <img src="/oliver-vuepress/middleware/redislearn/服务器启动时恢复文件选择.png" alt="foo"> <h3 id="rdb的文件格式"><a href="#rdb的文件格式" class="header-anchor">#</a> RDB的文件格式</h3> <img src="/oliver-vuepress/middleware/redislearn/RDB文件格式.png" alt="foo"> <h3 id="过期键处理"><a href="#过期键处理" class="header-anchor">#</a> 过期键处理</h3> <p><strong>生成RDB文件时</strong></p> <p>对于 已过期的键，将不会被保存到 新创建的 RDB文件中</p> <p><strong>载入RDB文件时</strong></p> <ul><li>主服务器模式：
<ul><li>过期键会被忽略</li></ul></li> <li>从服务器模式：
<ul><li>会将 RDB中的所有键（包含 过期键）都恢复，因为 主从同步、会从服务器的数据清空</li></ul></li></ul> <h2 id="aof"><a href="#aof" class="header-anchor">#</a> AOF</h2> <blockquote><p><code>AOF (Append Only File)</code> 持久化功能，默认是不开启的，<code>AOF</code>持久化是通过保存 Redis所有的执行写入命令。 这样 Redis 重启后 只要按照顺序回访这些命令就会恢复到原始状态</p> <p><strong>AOF</strong> 和 <strong>RDB</strong> 都开启了，redis重启的时候，也是优先通过 <strong>AOF</strong>进行数据恢复的，因为<strong>AOF</strong> 数据比较完整</p> <p>AOF 持久化功能的实现 可以分为 <code>命令追加</code>、<code>文件写入</code>、<code>文件同步</code></p></blockquote> <h3 id="文件写入-和-保存"><a href="#文件写入-和-保存" class="header-anchor">#</a> 文件写入 和 保存</h3> <img src="/oliver-vuepress/middleware/redislearn/AOF持久化文件的生成.png" alt="foo"> <center>AOF持久化文件的生成</center> <p>简单的可以理解为，每当client 端执行，非查询命令时，就会将其执行的命令，追加到 AOF 文件中</p> <center><font color="red">具体流程</font></center> <img src="/oliver-vuepress/middleware/redislearn/AOF具体流程.png" alt="foo"> <ol><li>命令追加：服务器每执行一次写命令都会以 协议格式 将执行的写命令追加到 <code>aof_buf</code> 缓冲区的尾部</li> <li>根据设定的规则，会进行异步将数据刷入磁盘中</li></ol> <p><strong>AOF 的 save/fsync 策略</strong></p> <p>有三种：</p> <ul><li>always: 每次写入数据，就立即将数据刷入磁盘</li> <li>everysec: 每隔1秒，将数据从 os cache 中写入磁盘 （每秒都是通过 fork 子进程进行写入）</li> <li>no：仅写入 os cache, 不会写入磁盘，然后根据系统自身的策略来决定什么时候刷入磁盘</li></ul> <p>Redis默认配置： <code>everysec</code></p> <h3 id="数据恢复"><a href="#数据恢复" class="header-anchor">#</a> 数据恢复</h3> <blockquote><p>AOF 恢复的过程相当简单，因为AOF文件里面包含了重建数据状态所需的所有写的命令，只需要服务根据顺序读入并重写执行一遍AOF文件里面所有的写命令即可</p></blockquote> <img src="/oliver-vuepress/middleware/redislearn/AOF数据恢复.png" alt="foo"> <h3 id="文件重写"><a href="#文件重写" class="header-anchor">#</a> 文件重写</h3> <p><strong>为什么要重写</strong></p> <p>随着命令的不断写入，数据会越来越多，文件会越来越大，所以需要瘦身，这样的好处在于，去除重复命令，较少数据，数据恢复时，熟读更快</p> <p><strong>重写那些命令</strong></p> <blockquote><p>将同一个key 的多条写入命令，重构成一条命令</p></blockquote> <p><strong>重写的配置 和 启动方式</strong></p> <ol><li>设置重写配置  <code>redis.conf</code> 进行配置</li></ol> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 表示当前aof文件大小超过上一次aof文件大小的百分之多少的时候会进行重写。如果之前没有重写过，以</span>
启动时aof文件大小为准
auto-aof-rewrite-percentage <span class="token number">100</span>

<span class="token comment"># 限制允许重写最小aof文件大小，也就是文件大小小于64mb的时候，不需要进行优化</span>
auto-aof-rewrite-min-size 64mb

</code></pre></div><ol start="2"><li>也可以通过手动输入 <code>bgrewriteaof</code> 命令 进行执行</li></ol> <p><strong>具体执行流程</strong></p> <img src="/oliver-vuepress/middleware/redislearn/AOF文件重写.png" alt="foo"> <ol><li>AOF文件大小超过配置的最大值时，会触发AOF文件的 <code>rewrite</code> 执行bgrewriteaof命令</li> <li>redis进程会fork一个子进程，子进程会带有主进程的数据副本，来构建新的AOF文件</li> <li>在rewrite期间，redis主进程会记录新的写入命令，并会开启AOF重写缓存</li> <li>子进程完成AOF重写之后，会向主进程发送一个完成信号</li> <li>主进程收到完成信号后，会将缓存池中的数据全部写入新的AOF文件中，对新的AOF文件改名，替换原有的AOF文件</li></ol> <h3 id="过期键处理-2"><a href="#过期键处理-2" class="header-anchor">#</a> 过期键处理</h3> <p><strong>写入AOF文件时</strong></p> <ul><li>过期，但未被 惰性删除 或 定期删除： 则不会产生任何影响</li> <li>过期， 被执行 惰性删除 或 定期删除： 则会想 AOF 追加一条 <code>DEL message</code> 命令</li></ul> <p><strong>AOF重写</strong></p> <p>已过期的键 不会被写到 新的AOF文件中</p> <p><strong>主从复制时</strong></p> <ul><li>主服务器： 删除一个过期键时，会同步发送信息给 从服务器删除</li> <li>从服务器：那么键过期也不会进行删除，只有接收到主服务器发送来的 删除信息 从服务器才会删除 (保持主从一致)</li></ul> <h3 id="相关问题"><a href="#相关问题" class="header-anchor">#</a> 相关问题</h3> <h4 id="aof重写-一定会是重写成一条吗"><a href="#aof重写-一定会是重写成一条吗" class="header-anchor">#</a> AOF重写 一定会是重写成一条吗?</h4> <p>一般情况下都会重写成一条</p> <p>但是有两种情况会生成多条：</p> <p>1、当执行 AOF文件重写时，由于 新的aof文件内容 = 重写后的 aof文件数据  + 重写时新增的缓冲区数据  （当 重写时，正好又有对相关key 进行操作时，则会在最后合并时存在多条）</p> <p>2、为了保证 <code>客户端输入缓存池溢出</code> （即一次操作过多的元素), Redis 设置了 指定参数 <code>REDIS_AOF_REWRITE_ITEMS_PER_CMD</code> = 64。 重写时会对 （列表、哈希表、集合、有序集合） 进行元素数量检测，超过改设置，则会拆分成多条</p> <h4 id="aof-重写使用子线程的好处"><a href="#aof-重写使用子线程的好处" class="header-anchor">#</a> AOF 重写使用子线程的好处？</h4> <ul><li>子线程的好处，避免锁、保证了数据的安全性</li></ul> <h2 id="rdb-和-aof-两者的优缺点"><a href="#rdb-和-aof-两者的优缺点" class="header-anchor">#</a> RDB 和 AOF 两者的优缺点：</h2> <h4 id="rdb-2"><a href="#rdb-2" class="header-anchor">#</a> RDB</h4> <ul><li>优点；
<ul><li>RDB 会生成多个数据文件，是更具某一时刻的redis数据，非常适合做冷备份</li> <li>RDB 是二进制压缩文件，占用空间小，便于传输</li> <li>RDB 对redis对外提供的读写服务, 影响非常小，可以让redis保持高性能，因为主进程只需要fork一个子进程，子进程来处理RDB持久化</li> <li>RDB 存放的是一份数据文件，恢复时，直接加载到内存中即可 （恢复过程更快）</li></ul></li> <li>缺点:
<ul><li>RDB 因为是存储的某个时刻的数据，不能保证数据的完整性</li> <li>如果 fork的子进程，数据文件特别大时，可能会导致暂停数毫秒或秒</li></ul></li></ul> <h4 id="aof-2"><a href="#aof-2" class="header-anchor">#</a> AOF</h4> <ul><li>优点：
<ul><li>AOF 可以更好的保证数据的完整性，不丢失数据，一般AOF 会每隔1秒进行fsync操作，最多丢失一秒的数据</li> <li>AOF 日志文件以 <code>append-only</code> 模式写入，对磁盘寻址没有任何开销，写入性能高，文件不易破损，即使尾部破损，也容易恢复</li> <li>AOF 文件过大，出现后台重写时，也不会用影响客户端的读写操作</li> <li>AOF 日志文件是记录所有的命令，所有非常适合做灾难性的误删除的紧急恢复 （当不小心用了flushall命令时，只要后台rewrite 还没发生，就能通过AOF文件，将数据都回访回去）</li></ul></li> <li>缺点：
<ul><li>AOF日志文件 通常比 RDB数据快照要更大</li> <li>AOF开启后的 写的QPS 会 比 RDB 的QPS低 （因为AOF每秒 fsync 一次，对性能的影响还是很大的）</li> <li>做恢复时，会比较慢（需要回放和执行指令）</li> <li>不方便做定期备份和 冷备份，需要手写脚本去恢复数据</li></ul></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#redis-的持久化" class="sidebar-link reco-side-redis-的持久化" data-v-cb1513f6>Redis 的持久化</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#rdb" class="sidebar-link reco-side-rdb" data-v-cb1513f6>RDB</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#什么是rdb" class="sidebar-link reco-side-什么是rdb" data-v-cb1513f6>什么是RDB?</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#触发生成快照时间-或-方式" class="sidebar-link reco-side-触发生成快照时间-或-方式" data-v-cb1513f6>触发生成快照时间 或 方式</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#生成-和-载入" class="sidebar-link reco-side-生成-和-载入" data-v-cb1513f6>生成 和 载入</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#rdb的文件格式" class="sidebar-link reco-side-rdb的文件格式" data-v-cb1513f6>RDB的文件格式</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#过期键处理" class="sidebar-link reco-side-过期键处理" data-v-cb1513f6>过期键处理</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#aof" class="sidebar-link reco-side-aof" data-v-cb1513f6>AOF</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#文件写入-和-保存" class="sidebar-link reco-side-文件写入-和-保存" data-v-cb1513f6>文件写入 和 保存</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#数据恢复" class="sidebar-link reco-side-数据恢复" data-v-cb1513f6>数据恢复</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#文件重写" class="sidebar-link reco-side-文件重写" data-v-cb1513f6>文件重写</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#过期键处理-2" class="sidebar-link reco-side-过期键处理-2" data-v-cb1513f6>过期键处理</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#相关问题" class="sidebar-link reco-side-相关问题" data-v-cb1513f6>相关问题</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E6%8C%81%E4%B9%85%E5%8C%96.html#rdb-和-aof-两者的优缺点" class="sidebar-link reco-side-rdb-和-aof-两者的优缺点" data-v-cb1513f6>RDB 和 AOF 两者的优缺点：</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/52.44e69a41.js" defer></script>
  </body>
</html>
